<?php
session_start();	
class cadastroQuestao extends Tpage{
	public $_vetor  = null;
		
	
	
	public function saveRecord($sender, $param){
		
		$questao = new questaoRecord();
		
		$tipoQuestao = $idDisciplina = $this->collectSelectionResult($this->Tipo);
		if($tipoQuestao == 1){
			$questao->BooleanIsDescritiva = false;	
		}else{
			$questao->BooleanIsDescritiva = true;
		}
		$questao->FKCodLongTema = $this->collectSelectionResult($this->Tema);
		
		$questao->FKCodLongUsuario = $_SESSION['idUsuario'];
		$questao->FKCodStrDisciplina = $this->collectSelectionResult($this->Disciplina);
		$questao->IntDificuldade = $this->collectSelectionResult($this->Dificuldade);
		$questao->IntTemp = $this->collectSelectionResult($this->Tempo);
		$questao->StrHtml = $this->Content->SafeText;
		
		$this->mensagen->Text ="cadastro efetuado com sucesso";
		
		$this->Content->Text = "";
		
		$questao->save();
		
		
	}
	
	public function onLoad($param) {
        parent::onLoad($param);
        if(!$this->IsPostBack)
        {
        	
        	
        	$this->usuarioLogado->Text = $_SESSION['usuario'];
        	
        	$_disciplina = disciplinaRecord::finder()->findAll();
			$_SESSION['disciplina'] = $_disciplina;	
			
			foreach ($_disciplina as $dados=>$valor){

				if($vetor === null){
					$vetor [] = (array(
									'PKCodStrDisciplina'=>"-1",
									'disciplina'=>"Selecione uma Disciplina",
					));
				}
				
				$vetor []= (array(
            					'PKCodStrDisciplina'=>$valor->PKCodStrDisciplina,
            					'disciplina'=>$valor->StrDisciplina,
				));
			}
            $data = $vetor;
           
			
            $this->Disciplina->DataSource=$data;
            $this->Disciplina->dataBind();
            
       
        	
        
        }else{
       		$this->buscaTemaByDisciplina();
       		//echo "to no buscarTema"; 	
        }
    }
    
	public function buscaTemaByDisciplina(){
		$idDisciplina = $this->collectSelectionResult($this->Disciplina);	
		
		$_tema = temaRecord::finder()->findAll('FKCodStrDisciplina = ?',$idDisciplina);	
        		
        		
        		foreach ($_tema as $dados=>$valor1){

				$vetor1 []= (array(
            					'PKCodLongTema'=>$valor1->PKCodLongTema,
            					'tema'=>$valor1->StrTema,
				));
			}
            $data1 = $vetor1;
            
           	
            
            $this->Tema->DataSource=$data1;
            $this->Tema->dataBind();
	} 

protected function collectSelectionResult($input)
    {
        $indices=$input->SelectedIndices;
        $result='';
        foreach($indices as $index)
        {
            $item=$input->Items[$index];
            $result = $item->Value;
        }
        return $result;
    }
}
?>